package com.xh3140.xwidget.dialog;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.lifecycle.MutableLiveData;

import com.xh3140.xwidget.dialog.listener.OnClickButtonListener;
import com.xh3140.xwidget.dialog.view.FooterView;
import com.xh3140.xwidget.dialog.view.HeaderView;
import com.xh3140.xwidget.dialog.view.RootView;

/**
 * 自定义对话框
 */
@SuppressWarnings("unused")
public class CustomDialog extends BaseDialog<View> {
  private View mCustomBodyView;
  private OnClickButtonListener<CustomDialog> mOnClickButtonListener;

  @NonNull
  @Override
  protected RootView<View> createRootView(@NonNull Context context) {
    final CustomDialog dialog = this;
    return new RootView<View>(context) {
      @Override
      protected HeaderView createHeaderView(@NonNull Context context) {
        return new HeaderView(context).configView(mHeaderParams);
      }

      @Override
      protected View createBodyView(@NonNull Context context) {
        return mCustomBodyView;
      }

      @Override
      protected FooterView createFooterView(@NonNull Context context) {
        return new FooterView(context).configView(dialog, mFooterParams, mOnClickButtonListener);
      }
    };
  }

  @Override
  public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    ViewModel.getDefault(this, ViewModel.class).configCustomDialogParams(this);
    return super.onCreateView(inflater, container, savedInstanceState);
  }

  /**
   * 数据状态保存
   * 由于屏幕方向旋转等情况会导致构建数据丢失，所以需要保存
   */
  public static final class ViewModel extends BaseDialog.ViewModel {
    private final MutableLiveData<View> mCustomBodyViewData = new MutableLiveData<>();
    private final MutableLiveData<OnClickButtonListener<CustomDialog>> mOnClickButtonListenerData = new MutableLiveData<>();

    private void configCustomDialogParams(@NonNull final CustomDialog dialog) {
      configBaseDialogParams(dialog);
      if (dialog.mCustomBodyView == null) {
        dialog.mCustomBodyView = mCustomBodyViewData.getValue();
      } else {
        mCustomBodyViewData.postValue(dialog.mCustomBodyView);
      }
      if (dialog.mOnClickButtonListener == null) {
        dialog.mOnClickButtonListener = mOnClickButtonListenerData.getValue();
      } else {
        mOnClickButtonListenerData.postValue(dialog.mOnClickButtonListener);
      }
    }
  }

  /**
   * 自定义对话框构建器
   * 设置自定义对话框属性
   */
  public static final class Builder extends BaseDialog.Builder<CustomDialog, Builder> {
    /**
     * 对话框自定义主体视图
     */
    private View mCustomBodyView;

    public Builder(@NonNull Context context) {
      super(context);
    }

    public Builder(@NonNull FragmentActivity activity) {
      super(activity);
    }

    public Builder(@NonNull Fragment fragment) {
      super(fragment);
    }

    /**
     * 构建对话框
     * 这个方法应该在所有配置结束后调用
     */
    @Override
    public CustomDialog build() {
      final CustomDialog dialog = new CustomDialog();
      dialog.mDialogParams = mDialogParams;
      dialog.mHeaderParams = mHeaderParams;
      dialog.mFooterParams = mFooterParams;
      dialog.mCustomBodyView = mCustomBodyView;
      dialog.mOnClickButtonListener = mOnClickButtonListener;
      return dialog;
    }

    /**
     * 设置自定义主体视图
     *
     * @param view 自定义主体视图
     */
    public Builder setCustomBodyView(@NonNull View view) {
      mCustomBodyView = view;
      return this;
    }

    /**
     * 设置自定义主体视图布局资源
     *
     * @param res 自定义主体视图布局资源
     */
    public Builder setBodyViewResource(@LayoutRes int res) {
      mCustomBodyView = LayoutInflater.from(mDialogParams.context).inflate(res, null, false);
      return this;
    }
  }
}
